package com.timothy.service;

import android.util.Log;

import com.timothy.answer.BaseAnswer;
import com.timothy.answer.BaseStationSelection;

public class MessageSenderUtil {
	
	private static final String LOG_TAG = "MessageSender";
	
	public static String PC_IP;
	public static int PC_PORT = 9999;
	
	public static String PHONE_IP; 
	public static Integer MODE; 
	
	//header message, to include phone IP address
	private static String HEADER = "<Header>" +
										"<Phone IPAddress=\"%s\" Name=\"\" Port=\""+PC_PORT+"\"></Phone>" +
									"</Header>";
	
	//Get base stations from server
	public static final String Get_Base_Stations = "<Message Type=\"GetBaseStations\">" +
													  "<Content></Content>" +
												   "</Message>";
 
	//Send selected base station to server
	public static final String Base_Station_Selection = "<Message Type=\"Authorize\">" +
        													"<Content>" + 
        														"<BaseStation>%s</BaseStation>"+
        														"<Password>%s</Password>"+
        													"</Content>" +
        												"</Message>";
	//Get demo sessions from server
	public static final String Get_Demo_Sessions = "<Message Type=\"GetSessions\">" +
													  "<Content></Content>" +
												   "</Message>";
	//Send selected base station to server
	public static final String Demo_Session_Selection = "<Message Type=\"SelectSession\">" +
        													"<Content>" + 
        														"<Session>%s</Session>"+
        													"</Content>" +
        												"</Message>";
	
	//Send single question select to server
	public static final String Single_Select_Answer = "<Message Type=\"QuestionResult\">" +
													"<Content>" + 
														"<SlideIndex>%d</SlideIndex>" +
														"<Value>%s</Value>" + 
													"</Content>" + 
													"</Message>";
	
	//Send multi question select to server
	public static final String Multi_Select_Answer = "<Message Type=\"QuestionResult\">"+
														    "<Content>" +
														          "<SlideIndex>%d</SlideIndex>" +
														          "%s" +
														    "</Content>" +
													 "</Message>";
	
	//request disconnect with server
	public static final String DISCONNECT = "<Message Type=\"Disconnect\">" + 
    											"<Content>" +
    											"</Content>" +
    										"</Message>";
	
	//request disconnect with server
	public static final String Next_Question = "<Message Type=\"NextQuestion\">" + 
    											"<Content>" +
    											"</Content>" +
    										"</Message>";
	
	public static String addHeader( String message ) {
		int index = message.indexOf('>')+1;
		return message.substring(0, index) + String.format(HEADER, PHONE_IP) + message.substring(index) ;
	}

	//send selected base station to server
	public static String sendBaseStationSelection(BaseStationSelection bss) {
		HEADER = HEADER.replaceAll("Name=\"[^\"]*\"", "Name=\""+bss.getUserName()+"\"");
		return bss.toMessage();
	}
	
	//send selected session to server
	public static String sendDemoSessionSelection(String session) {
		String sessionSelected = String.format( MessageSenderUtil.Demo_Session_Selection, session);
		return addHeader(sessionSelected);
	}
	
	public static String transferAnswerToString( BaseAnswer answer ) {
		String message = answer.toMessage();
		Log.i(LOG_TAG,"sendAnswer:"+message);
		return addHeader(message); 
	}
	
	public static boolean sendDisconnectMessage(){
		return false; 
	}
	
	public static String sendNextQuestion() {
		return addHeader(Next_Question); 
	}
}
